package ink.onei.parse.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import ink.onei.common.core.utils.poi.ExcelUtil;
import ink.onei.common.core.web.controller.BaseController;
import ink.onei.common.core.web.domain.AjaxResult;
import ink.onei.common.core.web.page.TableDataInfo;
import ink.onei.common.log.annotation.Log;
import ink.onei.common.log.enums.BusinessType;
import ink.onei.common.security.annotation.RequiresPermissions;
import ink.onei.parse.domain.TdRawWaterType;
import ink.onei.parse.listener.DataListener;
import ink.onei.parse.service.ITdRawWaterTypeService;
import ink.onei.parse.service.serviceImpl.ExcelDAOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 原料水检测分类Controller
 *
 * @author ruoyi
 * @date 2024-03-16
 */
@Slf4j
@RestController
@RequestMapping("/type")
public class TdRawWaterTypeController extends BaseController {
    @Autowired
    private ITdRawWaterTypeService tdRawWaterTypeService;

    @Autowired
    private ExcelDAOService excelDAOService;

    /**
     * 查询原料水检测分类列表
     */
    @RequiresPermissions("wastewaterdataimport:type:list")
    @GetMapping("/list")
    public TableDataInfo list(TdRawWaterType tdRawWaterType) {
        startPage();
        List<TdRawWaterType> list = tdRawWaterTypeService.selectTdRawWaterTypeList(tdRawWaterType);
        return getDataTable(list);
    }

    /**
     * 导出原料水检测分类列表
     */
    @RequiresPermissions("wastewaterdataimport:type:export")
    @Log(title = "原料水检测分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdRawWaterType tdRawWaterType) {
        List<TdRawWaterType> list = tdRawWaterTypeService.selectTdRawWaterTypeList(tdRawWaterType);
        ExcelUtil<TdRawWaterType> util = new ExcelUtil<TdRawWaterType>(TdRawWaterType.class);
        util.exportExcel(response, list, "原料水检测分类数据");
    }

    /**
     * 获取原料水检测分类详细信息
     */
    @RequiresPermissions("wastewaterdataimport:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tdRawWaterTypeService.selectTdRawWaterTypeById(id));
    }

    /**
     * 新增原料水检测分类
     */
    @RequiresPermissions("wastewaterdataimport:type:add")
    @Log(title = "原料水检测分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdRawWaterType tdRawWaterType) {
        return toAjax(tdRawWaterTypeService.insertTdRawWaterType(tdRawWaterType));
    }

    /**
     * 修改原料水检测分类
     */
    @RequiresPermissions("wastewaterdataimport:type:edit")
    @Log(title = "原料水检测分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdRawWaterType tdRawWaterType) {
        return toAjax(tdRawWaterTypeService.updateTdRawWaterType(tdRawWaterType));
    }

    /**
     * 删除原料水检测分类
     */
    @RequiresPermissions("wastewaterdataimport:type:remove")
    @Log(title = "原料水检测分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tdRawWaterTypeService.deleteTdRawWaterTypeByIds(ids));
    }

    /**
    * 文件上传
    **/
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "File is empty.";
        }
        try {
            // 获取上传的Excel文件流
            InputStream inputStream = file.getInputStream();
            // 使用EasyExcel解析Excel文件
            EasyExcel.read(inputStream, TdRawWaterType.class,new DataListener()).excelType(ExcelTypeEnum.XLSX).sheet().headRowNumber(3).doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return "An error occurred while processing the file.";
        }
        return "Excel file uploaded and validated successfully.";

    }
}
